import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()]
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
  server: {
    proxy: {
      // 用户服务代理
      '/api/apply': {
        target: 'http://localhost:8081',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/apply/, '/api/apply')
      },
      '/api/reimburse': {
        target: 'http://localhost:8081',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/reimburse/, '/api/reimburse')
      },
      '/api/work': {
        target: 'http://localhost:8082',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api\/work/, '/api/work')
      },
      // 工作列表API代理
      '/work': {
        target: 'http://localhost:8082',
        changeOrigin: true,
        secure: false
        // 移除rewrite规则，让/work前缀保留
      },
      // 操作日志API代理
      '/opera': {
        target: 'http://localhost:8082',
        changeOrigin: true,
        secure: false
        // 移除rewrite规则，让/opera前缀保留
      },
      // 部门API代理
      '/department': {
        target: 'http://localhost:8082',
        changeOrigin: true,
        secure: false
        // 移除rewrite规则，让/department前缀保留
      },
      // HR人事API代理
      '/hr': {
        target: 'http://localhost:8082',
        changeOrigin: true,
        secure: false
        // 移除rewrite规则，让/hr前缀保留
      },
      '/api': {
        target: 'http://localhost:8082',
        changeOrigin: true,
        secure: false,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  }
})
